﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PropertyManagement.Core.DataAccess;
using PropertyManagement.Core.Model;

namespace PropertyManagement.Core {
    public class MaintainManage {
        public Maintain GetById(int maintainId) {
            return MaintainDataAccess.GetById(maintainId);
        }

        public Maintain LoadByCode(string maintainCode) {
            return MaintainDataAccess.GetByPKs(new Maintain {MaintainCode = maintainCode});
        }

        public void Update(Maintain maintain) {
            MaintainDataAccess.Update(maintain);
        }

        public void Add(Maintain currentMaintain) {
            if (!MaintainDataAccess.ExistsByPKs(currentMaintain)) {
                if (currentMaintain.ApplyDate == default (DateTime)) {
                    currentMaintain.ApplyDate = DateTime.Today;
                }
                if (currentMaintain.RepaireDate == default (DateTime)) {
                    currentMaintain.RepaireDate = DateTime.MaxValue;
                }
                currentMaintain.LastUpdate = currentMaintain.CreationDate = DateTime.Now;
                currentMaintain.LastActionUserID = currentMaintain.CreationUserID = StaticProvider.Instance.CurrentLoginedUser.SystemUserID;
                MaintainDataAccess.Add(currentMaintain);
            }
           
        }

        public void Remove(Maintain maintain) {
            MaintainDataAccess.Remove(maintain);
        }
    }
}
